package com.longxi.tianjintianbao.ui.presenter.news;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Toast;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.listener.AdapterListener;
import com.longxi.tianjintianbao.model.task.Task;
import com.longxi.tianjintianbao.model.work.News;
import com.longxi.tianjintianbao.ui.activity.main.BrowserActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.news.NewsViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.news.INewDetailAtView;
import com.longxi.tianjintianbao.ui.view.news.INewsAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.NListRefresh;
import com.zzhoujay.richtext.RichText;
import com.zzhoujay.richtext.callback.OnUrlClickListener;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class NewDetailAtPresenter extends BasePresenter<INewDetailAtView> {

    private News news;

    public NewDetailAtPresenter(BaseActivity context) {
        super(context);
    }

    public void setBar() {
        mContext.setTitle("新闻详情");
        mContext.setTvLeft(MyUtils.getString(R.string.back));
        setDate();
    }

    public void setDate(){
        news = (News) mContext.getIntent().getSerializableExtra(AppConst.NEWS);

        getView().getTvTitle().setText(news.getTaile());
        getView().getTvTimeSource().setText("发布时间:" + news.getSenddate() + " 发布人:" + news.getSender());

        String infoRichText = news.getContent();
        RichText.initCacheDir(mContext.getApplicationContext());
        RichText.from(infoRichText)
        .urlClick(new OnUrlClickListener() {
            @Override
            public boolean urlClicked(String url) {
                if (url.startsWith("code://")) {
                    Toast.makeText(mContext, url.replaceFirst("code://", ""), Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }
        })
        .into(getView().getTvContent());

//        setAttachmentName("慧管.dox");
    }

    private void setAttachmentName(String attachmentName){
        if(attachmentName.isEmpty() && "".equals(attachmentName)){
            getView().getTvAttachmentName().setVisibility(View.GONE);
            return;
        }
        String tip = "下载附件: ";
        SpannableStringBuilder spannable = new SpannableStringBuilder(tip + attachmentName);
        spannable.setSpan(new ForegroundColorSpan(MyUtils.getResource().getColor(R.color.color_red1)),tip.length(),spannable.length()
                , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        getView().getTvAttachmentName().setMovementMethod(LinkMovementMethod.getInstance());
        spannable.setSpan(new TextClick(),tip.length(),spannable.length()
                , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        getView().getTvAttachmentName().setText(spannable);
        getView().getTvAttachmentName().setVisibility(View.VISIBLE);
    }

    private class TextClick extends ClickableSpan {
        @Override
        public void onClick(View widget) {
            //在此处理点击事件
//            Intent intent = new Intent(mContext, BrowserActivity.class);
//            intent.putExtra("url", "http://view.officeapps.live.com/op/view.aspx?src=" + "newteach.pbworks.com%2Ff%2Fele%2Bnewsletter.docx");
            Uri uri = Uri.parse("http://view.officeapps.live.com/op/view.aspx?src=" + "newteach.pbworks.com%2Ff%2Fele%2Bnewsletter.docx");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            mContext.startActivity(intent);
        }
    }

    @Override
    public void detachView() {
        super.detachView();
        RichText.recycle();
    }

}
